package ext.generic.wfaugment;


import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import wt.fc.Persistable;
import wt.method.RemoteAccess;
import wt.method.RemoteMethodServer;
import wt.util.WTException;
import wt.util.WTProperties;
import wt.workflow.engine.WfProcess;
import wt.workflow.work.WfAssignedActivity;
import wt.workflow.work.WorkItem;

import com.ptc.netmarkets.util.beans.NmCommandBean;

import ext.generic.wfaugment.processors.WfAugmentUtil;

/**
 *流程常用方法
 */
public class GetPageUrl implements RemoteAccess
{
    public static boolean VERBOSE = true;
    public static WTProperties wtProperties;
    public static String CLASSNAME = "";
    private static String configFilePath = "";
    private static String sheetName = "WFTemplate";

    static
    {
        CLASSNAME = GetPageUrl.class.getName();
        try
        {
            wtProperties = WTProperties.getLocalProperties();
           // VERBOSE = wtProperties.getProperty("ext.generic.print.verbose", false);
            //通用客制化模版XSL路径
            configFilePath = wtProperties.getProperty("ext.generic.WFTemplate", "");
        }
        catch (java.io.IOException ioe)
        {
            ioe.printStackTrace();
        }
    }

    /**
     *客制化流程中获取配置的子jsp页面的URL
     * @param nmcommandbean
     * @return
     * @throws WTException
     */
    public static String getSubPageUrl(NmCommandBean nmcommandbean) throws WTException
    {
        String subPageUrl = "";
        if (!RemoteMethodServer.ServerFlag)
        {
            String method = "doGetSubPageUrl";
            try
            {
                subPageUrl = (String) RemoteMethodServer.getDefault().invoke(method, CLASSNAME, null, new Class[] { NmCommandBean.class }, new Object[] { nmcommandbean });
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return subPageUrl;
        }
        else
            return doGetSubPageUrl(nmcommandbean);
    }

    /**
     *客制化流程中获取配置的子jsp页面的URL的具体方法
     * @param nmcommandbean
     * @return
     * @throws WTException
     */
    public static String doGetSubPageUrl(NmCommandBean nmcommandbean) throws WTException
    {
        String subPageUrl = "";
        String activityName = "";
        String wfProcessTemplateName = "";
        Persistable persistable = nmcommandbean.getPrimaryOid().getWtRef().getObject();
        if (persistable instanceof WorkItem)
        {
            WorkItem workItem = (WorkItem) persistable;
            WfAssignedActivity wfassignedActivity = (WfAssignedActivity) workItem.getSource().getObject();
            activityName = wfassignedActivity.getName();
            WfProcess wfProcess = wfassignedActivity.getParentProcess();
            wfProcessTemplateName = wfProcess.getTemplate().getName();
            subPageUrl = getSubPageUrl(wfProcessTemplateName, activityName);
            if (VERBOSE)
            {
                System.out.println("	>>>doGetSubPageUrl	subPageUrl:" + subPageUrl);
            }
        }
        return subPageUrl;
    }

    /**
     *客制化流程中获取配置的子jsp页面的URL的具体方法
     * @param wfProcessTemplateName 流程模板名称
     * @param activityName 流程任务节点名称
     * @return
     * @throws WTException
     */
    public static String getSubPageUrl(String wfProcessTemplateName, String activityName) throws WTException
    {
        if (VERBOSE)
        {
            System.out.println("	>>>getSubPageUrl	wfProcessTemplateName:" + wfProcessTemplateName + "	activityName:" + activityName);
            System.out.println("	>>>getSubPageUrl	configFilePath:" + configFilePath);
        }

        if (configFilePath == null || configFilePath.length() == 0)
            throw new WTException("没有找到configFilePath,请确认site.xconf文件中ext.MOON.workflow.WFTemplateGuide属性的配置!");
        XSSFWorkbook wb = null;
        FileInputStream fis = null;
        try
        {
            fis = new FileInputStream(configFilePath);
            wb = new XSSFWorkbook(fis);
        }
        catch (IOException ioe)
        {
            wb = null;
        }
        finally
        {
            try
            {
                fis.close();
            }
            catch (IOException ioe2)
            {
                throw new WTException("关闭配置文件出现异常!");
            }
        }
        if (wb == null)
            throw new WTException("没有找到configFilePath对应的配置文件!");
/*        if(wfProcessTemplateName.equals("GenericPartWf")){
        	sheetName="part";
        }else if(wfProcessTemplateName.equals("GenericPartWfWithES")){
        	sheetName="part_esignature";
        }else if(wfProcessTemplateName.equals("GenericDocWf")){
        	sheetName="doc";
        }else if(wfProcessTemplateName.equals("GenericDocWfWithES")){
        	sheetName="doc_esignature";
        }else if(wfProcessTemplateName.equals("GenericECNWf")){
        	sheetName="ecn";
        }else{
        	
        } 
*/       
        XSSFSheet sheet = wb.getSheet(sheetName);
        if (sheet == null)
            throw new WTException("没有找到configFilePath对应的配置文件名为:" + sheetName + "的sheet!");
        Iterator rows = sheet.rowIterator();
        int rowNumber = 0;
        XSSFRow row;
        XSSFCell cell;

        int nSheetRow = sheet.getLastRowNum();

        if (VERBOSE)
        {
            System.out.println("	>>>getSubPageUrl	nSheetRow:" + nSheetRow);
        }
        // 读取配置文件
        for (int i = 0; i < nSheetRow; i++)
        {
            // 工作流模板名
            String s = WfAugmentUtil.getExcelCellContent(sheet, i, 0);           
            if (VERBOSE)
            {
                System.out.println("	>>>getSubPageUrl	找到工作流模板:" + s);
                System.out.println("	>>>wfProcessTemplateName	找到工作流模板:" + wfProcessTemplateName);
            }
            if (s.equalsIgnoreCase(wfProcessTemplateName))
            {
                // 活动名
                for (int j = i + 1; j < nSheetRow; j++)
                {
                    String ss = WfAugmentUtil.getExcelCellContent(sheet, j, 1);
                    if (VERBOSE)
                    {
                        System.out.println("	>>>getSubPageUrl	找到工作流模板:" + s + "	找到活动:" + ss);
                    }
                    // 到达下一个工作流模板时跳出循环
                    if (ss == null || ss.length() == 0)
                        break;
                    else if (ss.equalsIgnoreCase(activityName))
                    {
                        rowNumber = j;
                        break;
                    }
                }
            }
            if (rowNumber != 0)
                break;
        }
        if (rowNumber == 0)
        {
            throw new WTException("配置文件中没有对工作流模板名:" + wfProcessTemplateName + " 活动名:" + activityName + " 进行配置!");
        }

        if (VERBOSE)
        {
            System.out.println("	<<<getSubPageUrl	wfProcessTemplateName:" + wfProcessTemplateName + "	activityName:" + activityName);
            System.out.println("	<<<getSubPageUrl	configFilePath:" + configFilePath);
        }

        return WfAugmentUtil.getExcelCellContent(sheet, rowNumber, 2);

    }

   
}
